package com.dzl.integral;

import com.dzl.base.controller.BaseController;
import com.dzl.integral.pojo.WelfareIntegralGrantRecordRel;
import com.dzl.integral.pojo.WelfareIntegralProcess;
import com.dzl.integral.pojo.WelfareIntegralRecord;
import com.dzl.integral.pojo.dto.WelfareIntegralProcessDto;
import com.dzl.integral.pojo.dto.WelfareIntegralRechargeDto;
import com.dzl.integral.pojo.query.WelfareIntegralRecordQuery;
import com.dzl.integral.pojo.vo.WelfareIntegralRecordVo;
import com.dzl.integral.service.WelfareIntegralGrantRecordRelService;
import com.dzl.integral.service.WelfareIntegralProcessService;
import com.dzl.integral.service.WelfareIntegralRecordService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 积分充值管理 - 控制层
 * @author WangJian
 * @date 2021/9/6 0006
 */
@RestController
@RequestMapping("/integralRecord")
public class WelfareIntegralRecordController extends BaseController {

    @Autowired
    private WelfareIntegralRecordService service;
    @Autowired
    private WelfareIntegralGrantRecordRelService welfareIntegralGrantRecordRelService;
    @Autowired
    private WelfareIntegralProcessService welfareIntegralProcessService;

    /**
     * 积分充值（审核）记录管理列表
     * @date 2021/9/6 0006 16:30
     * @author wangjian
     * @param welfareIntegralRecordQuery 积分审核列表的查询条件
     * @return com.dzl.util.PageUtils<com.dzl.integral.pojo.vo.WelfareIntegralRecordVo>
     */
    @GetMapping("/selectIntegralRecordList")
    public PageUtils<WelfareIntegralRecordVo> selectIntegralRecordList(WelfareIntegralRecordQuery welfareIntegralRecordQuery){
        PageInfo<WelfareIntegralRecordVo> pageInfo =  service.selectIntegralRecordListPage(welfareIntegralRecordQuery);
        return new PageUtils<> (pageInfo);
    }

    /**
     * 充值积分
     * @date 2021/9/6 0006 16:32
     * @author wangjian
     * @param dto 积分充值对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/integralRecharge")
    public JsonResult<Integer> integralRecharge(@RequestBody WelfareIntegralRechargeDto dto){
        if (EmptyUtils.isEmpty(dto.getEnterpriseId())){
            return JsonResult.failWithMsg("请选择公司");
        }
        if (EmptyUtils.isEmpty(dto.getSalesUserId())){
            return JsonResult.failWithMsg("请选择业务员");
        }
        if (EmptyUtils.isEmpty(dto.getRechargeIntegral()) || dto.getRechargeIntegral().compareTo(BigDecimal.ZERO) < 0){
            return JsonResult.failWithMsg("请输入正确数字积分");
        }
        return JsonResult.success(service.saveIntegralRecharge(dto,getAdminInfo().getUserName()));
    }

    /**
     * 审核充值的积分
     * @date 2021/9/6 0006 17:29
     * @author wangjian
     * @param dto 积分充值对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/saveIntegralProcess")
    public JsonResult<Integer> saveIntegralProcess(@RequestBody WelfareIntegralProcessDto dto){
        //校验参数
        if (EmptyUtils.isEmpty(dto.getReason())){
            return JsonResult.failWithMsg("请填写原因！");
        }
        //获取该笔积分当前所在的状态
        WelfareIntegralRecord welfareIntegralRecord = service.selectByPrimaryKey(dto.getIntegralRecordId());
        Integer status = welfareIntegralRecord.getStatus();
        Integer rechargeStatus = 0;
        if (dto.getStatus() == 2 ){//审核通过
            rechargeStatus = 2;
            if (status != 1){
                return JsonResult.failWithMsg("审核流程错误！");
            }
        }else if (dto.getStatus() == 3 ){//3拒绝审核
            rechargeStatus = 4;
            if (status != 1 ){
                return JsonResult.failWithMsg("审核流程错误！");
            }
        }else if (dto.getStatus() == 4 ){//4 取消充值
            rechargeStatus = 4;
            if (status != 2){
                return JsonResult.failWithMsg("审核流程错误！");
            }

        } else if (dto.getStatus() == 5) {//5 撤回充值
            rechargeStatus = 5;
            if (status != 3){
                return JsonResult.failWithMsg("审核流程错误！");
            }
            //取消充值需判断充值的积分是否被分发
            WelfareIntegralGrantRecordRel welfareIntegralGrantRecordRel = new WelfareIntegralGrantRecordRel();
            welfareIntegralGrantRecordRel.setRecordId(dto.getIntegralRecordId());
            int i = welfareIntegralGrantRecordRelService.selectCount(welfareIntegralGrantRecordRel);
            if (i > 0){
                return JsonResult.failWithMsg("积分已分发至成员，不能撤回！");
            }
        } else {
            return JsonResult.failWithMsg("审核流程错误！");
        }
        dto.setCreateName(getAdminInfo().getUserName());
        dto.setCreateTime(new Date());
        dto.setRechargeStatus(rechargeStatus);
        return JsonResult.success(service.saveIntegralProcess(dto));
    }

    /**
     * 查看审核进度
     * @date 2021/9/8 0008 16:23
     * @author wangjian
     * @param id 积分列表ID
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.integral.pojo.WelfareIntegralProcess>>
     */
    @GetMapping("/selectIntegralProcessList")
    public JsonResult<List<WelfareIntegralProcess>> selectIntegralProcessList(Integer id) {
        WelfareIntegralProcess welfareIntegralProcess = new WelfareIntegralProcess();
        welfareIntegralProcess.setIntegralRecordId(id);
        List<WelfareIntegralProcess> select = welfareIntegralProcessService.select(welfareIntegralProcess);
        select = select.stream().sorted(Comparator.comparing(WelfareIntegralProcess::getCreateTime).reversed()).collect(Collectors.toList());
        return JsonResult.success(select);
    }

    /**
     * 获取各状态下的充值记录数
     * @date 2021/9/8 0008 16:41
     * @author wangjian
     * @param welfareIntegralRecordQuery 查询条件，同列表查询条件
     * @return com.dzl.util.JsonResult<java.util.List<java.lang.Integer>>
     */
    @GetMapping("/selectIntegralRecordCount")
    public JsonResult<List<Integer>> selectIntegralRecordCount(WelfareIntegralRecordQuery welfareIntegralRecordQuery){
        return JsonResult.success(service.selectIntegralRecordCount(welfareIntegralRecordQuery));
    }
}
